package com.kaoyaya.delivery.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;

import com.googlecode.androidannotations.annotations.*;
import com.googlecode.androidannotations.annotations.sharedpreferences.Pref;
import com.kaoyaya.delivery.data.Constants;
import com.kaoyaya.delivery.data.DataHelper;
import com.kaoyaya.delivery.data.InfoFile_;

@NoTitle
@EActivity(R.layout.activity_loading)
public class LoadingActivity extends BaseActivity {

	@Pref InfoFile_ infoFile;
	@Bean DataHelper dataHelper;
	final int IS_Gold = 0, IS_AUTH = 1;
	
	@AfterViews
	void init() {
		if (getNetworkState()) {
			dataHelper.setCallbackHandler(callbackHandler);
			if (!infoFile.authGlobal().get()) {
				dataHelper.readSingleStr(IS_AUTH, "http://goven.googlecode.com/svn/app_auth/kaoyaya_auth.txt");
			} else {
				forward();
			}
		} else {
			Dialog dialog = new AlertDialog.Builder(LoadingActivity.this).setTitle("检测到当前网络不可用").setMessage("是否跳转到网络配置界面？")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {
							// 跳转到无线网络配置界面
							startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
							LoadingActivity.this.finish();
						}
					}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							LoadingActivity.this.finish();
						}
					}).setCancelable(false).create();// 创建按钮
			dialog.show();
		}
	}
	
	void handleAuth(String auth) {
		try {
			JSONObject jsonObj = new JSONObject(auth);
			infoFile.authGlobal().put(jsonObj.getBoolean("global"));
			infoFile.authSingle().put(jsonObj.getBoolean("single"));
			infoFile.authCombo().put(jsonObj.getBoolean("combo"));
			infoFile.authAlipy().put(jsonObj.getBoolean("alipy"));
			infoFile.authDelivery().put(jsonObj.getBoolean("delivery"));
		} catch (JSONException e) {
			infoFile.authGlobal().put(false);
			infoFile.authSingle().put(true);
			infoFile.authCombo().put(true);
			infoFile.authAlipy().put(true);
			infoFile.authDelivery().put(true);
		}
		forward();
	}
	
	boolean getNetworkState() {
		// 获取系统的连接服务
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		// 获取网络的连接情况
		NetworkInfo NetworkInfo = connectivityManager.getActiveNetworkInfo();
		if (NetworkInfo != null && NetworkInfo.isConnected()) {
			// 当前的网络连接可用
			return true;
		} else {
			// 当前的网络连接不可用
			return false;
		}
	}
	
	void forward() {
		Constants.authGlobal = infoFile.authGlobal().get();
		Constants.authSingle = infoFile.authSingle().get();
		Constants.authCombo = infoFile.authCombo().get();
		Constants.authAlipy = infoFile.authAlipy().get();
		Constants.authDelivery = infoFile.authDelivery().get();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		if (!infoFile.isAutoLogin().get()) {
			startActivity(new Intent(context, UserLoginActivity_.class));
		} else {
			startActivity(new Intent(context, SlidingActivity.class));
		}
		finish();
	}
	
	Handler callbackHandler = new Handler() {
		public void handleMessage(Message msg) {
			Object result = msg.obj;
			switch (msg.what) {
			case IS_AUTH:
				handleAuth((String) result);
				break;
			}
		}
	};

}